
const product = (state = [{ foods: [] }], action) => {
    switch (action.type) {
        case 'GET_LIST':
            return [...action.payload];
        case 'ADD':
            let { productItem, listItem } = action.payload
            let arr = state.find(item => item.name === listItem.name)
           
            let res = arr.foods.find(pro => pro.name === productItem.name)
            // ++res.count
            res.totalPrice=(++res.count)*res.price
            return [...state]
        case 'DEL':
            let { productItem1, listItem1 } = action.payload
            let arr1 = state.find(item => item.name === listItem1.name)
           
            let res1 = arr1.foods.find(pro => pro.name === productItem1.name)
            // --res1.count
            res1.totalPrice=(--res1.count)*res1.price
            // if(res1.count==0){

            // }
            return [...state]
        default:
            // console.log(state)
            return state;
    }
}

const gocart=(state = {price:0,count1:0,arr:[]},action)=>{
    switch(action.type){
        case "MAKE_ORDER":
         let arr = [];
         let price=0;
         let count1=0;
         action.payload.forEach(item => {
                item.foods.forEach(val => {
                    if (val.count != 0) {
                       price+=val.totalPrice;
                       count1+=val.count;
                       arr.push(val.name)
                    }
                })
            });
        state={price:price,count1:count1,arr:arr}
        console.log(price,count1,arr)
        return state
        default:
        return state
    }
}

const Login=function(state={type:0},action){
    console.log(action.payload)
    switch(action.type){
        case "LOGSUBMIT":
        state.type=action.payload
        return state;
        default:
        return state;
    }
}


const reducer = (state = {}, action) => {
    let flag=action.payload || 0
    return {
        products: product(state.products, action),
        carts:gocart(state.carts,action),
        Logins:Login(state.Logins,action),
        type:flag
    }
}

export default reducer;
